package com.my.zxsyd.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.zxsyd.common.ResultEnum;
import com.my.zxsyd.entity.Xygl;
import com.my.zxsyd.entity.Zuowei;
import com.my.zxsyd.entity.Zworder;
import com.my.zxsyd.entity.view.XyglView;
import com.my.zxsyd.entity.view.ZworderView;
import com.my.zxsyd.exception.MyException;
import com.my.zxsyd.mapper.ZworderMapper;
import com.my.zxsyd.qpage.QPage;
import com.my.zxsyd.service.IXyglService;
import com.my.zxsyd.service.IZuoweiService;
import com.my.zxsyd.service.IZworderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.zxsyd.util.ResultView;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 座位预定表 服务实现类
 * </p>
 *
 * @author lbk
 * @since 2024-02-20
 */
@Service
public class ZworderServiceImpl extends ServiceImpl<ZworderMapper, Zworder> implements IZworderService {

    @Autowired
    private IZuoweiService iZuoweiService;

    @Autowired
    private IXyglService iXyglService;

    @Override
    public ResultView getPageList(QPage qPage, ZworderView obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        List<ZworderView> mapList = this.baseMapper.getPageList(iPage,obj);
        return ResultView.ok(iPage.setRecords(mapList));
    }

    @Override
    public ZworderView getByInfoId(Integer id) {
        return baseMapper.getByInfoId(id);
    }

    @Override
    public ResultView yuding(ZworderView obj) {
        //验证座位状态
        Zuowei zuowei=this.iZuoweiService.getById(obj.getZwid());
        if(zuowei==null){
            throw new MyException(ResultEnum.CODE_2,"座位数据异常");
        }
        if(zuowei.getStatus()==2){
            throw new MyException(ResultEnum.CODE_2,"座位已被人预约，不可预定");
        }
        //检测当前用户是否违规3次以上
        int count=this.iXyglService.getXyMonth(obj.getYhid());
        if(count>=3){
            throw new MyException(ResultEnum.CODE_2,"用户信用记录违规次数超过当月3次，不可预定");
        }
        Zworder dto=new Zworder();
        BeanUtils.copyProperties(obj,dto);
        dto.setCtime(new Date());
        boolean isOk=this.save(dto);
        if(!isOk){
            throw new MyException(ResultEnum.CODE_2,"新增数据异常");
        }
        return ResultView.ok();
    }

    @Override
    public ResultView zhifu(Integer id) {
        Zworder zworder=this.getById(id);
        if(zworder==null){
            throw new MyException(ResultEnum.CODE_2,"座位订单数据异常");
        }
        if(zworder.getStatus()==2){
            throw new MyException(ResultEnum.CODE_2,"订单已超过规定时间自动取消，无法支付");
        }
        if(zworder.getPaystatus()==2){
            throw new MyException(ResultEnum.CODE_2,"订单已支付，无须重复支付");
        }
        zworder.setPaystatus(2);
        this.updateById(zworder);
        //更改座位状态和订单数据
        Zuowei zuowei=this.iZuoweiService.getById(zworder.getZwid());
        if(zuowei==null){
            throw new MyException(ResultEnum.CODE_2,"座位数据异常");
        }
        zuowei.setStatus(2);
        this.iZuoweiService.updateById(zuowei);
        return ResultView.ok();
    }

    @Override
    public ResultView cancel(Integer id) {
        Zworder zworder=this.getById(id);
        if(zworder==null){
            throw new MyException(ResultEnum.CODE_2,"座位订单数据异常");
        }
        if(zworder.getPaystatus()==1){
            throw new MyException(ResultEnum.CODE_2,"订单未支付，座位未锁定，无须取消预约");
        }
        zworder.setStatus(2);
        this.updateById(zworder);
        //释放座位
        //更改座位状态和订单数据
        Zuowei zuowei=this.iZuoweiService.getById(zworder.getZwid());
        if(zuowei==null){
            throw new MyException(ResultEnum.CODE_2,"座位数据异常");
        }
        zuowei.setStatus(1);
        this.iZuoweiService.updateById(zuowei);
        return ResultView.ok();
    }

    @Override
    public ResultView up(Integer id,Integer zwid) {
        Zworder zworder=this.getById(id);
        if(zworder==null){
            throw new MyException(ResultEnum.CODE_2,"座位订单数据异常");
        }
        if(zworder.getPaystatus()==1){
            throw new MyException(ResultEnum.CODE_2,"订单未支付，无法更换座位");
        }
        Zuowei zuowei=this.iZuoweiService.getById(zwid);
        if(zuowei==null){
            throw new MyException(ResultEnum.CODE_2,"座位数据异常");
        }
        if(zuowei.getStatus()==2){
            throw new MyException(ResultEnum.CODE_2,"座位已被人预约，不可预定");
        }
        //释放之前座位
        Zuowei oldzw=this.iZuoweiService.getById(zworder.getZwid());
        oldzw.setStatus(1);
        this.iZuoweiService.updateById(oldzw);
        //加锁现在更换的座位
        zworder.setZwid(zwid);
        this.updateById(zworder);
        return ResultView.ok();
    }


}
